//
//  DHS2_PreOrder_ItemCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/10.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHS2_PreOrder_ItemCell.h"
#import "DHCart_LeasedItemCell.h"

@interface DHS2_PreOrder_ItemCell()<UITableViewDelegate, UITableViewDataSource>

@property (strong, nonatomic) NSDictionary *orderDetailDic;
@property (strong, nonatomic) NSMutableArray *leasedArr;
@property (strong, nonatomic) NSMutableArray *purchasedArr;

@property (assign, nonatomic) BOOL isSix;

@end

@implementation DHS2_PreOrder_ItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.isSix = NO;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"DHCart_LeasedItemCell" bundle:nil] forCellReuseIdentifier:kDHCart_LeasedItemCell];
    
    WeakSelf(weakSelf)
    [self.sixButton bk_addEventHandler:^(id sender) {
        if (weakSelf.isSix) {
            [[DHNetAPIClient shareDHNetAPIClient] request_order_expressWithParams:@{@"order_id":[weakSelf.orderDetailDic objectForKey:@"_id"],@"need_fast":@"false"} andBlock:^(id data, NSError *error) {
                [weakSelf.sixButton setImage:[UIImage imageNamed:@"unselected"] forState:UIControlStateNormal];
                weakSelf.isSix = NO;
            }];
        }else{
            [[DHNetAPIClient shareDHNetAPIClient] request_order_expressWithParams:@{@"order_id":[weakSelf.orderDetailDic objectForKey:@"_id"],@"need_fast":@"true"} andBlock:^(id data, NSError *error) {
                [weakSelf.sixButton setImage:[UIImage imageNamed:@"selected"] forState:UIControlStateNormal];
                weakSelf.isSix = YES;
            }];
        }
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)updateCell:(NSDictionary *)dic{
    self.orderDetailDic = dic;
    self.leasedArr = [dic objectForKey:@"subscribed"];
    self.purchasedArr = [dic objectForKey:@"purchased"];
    
    NSDictionary *receiver = [self.orderDetailDic objectForKey:@"receiver"];
    if ([receiver objectForKey:@"phone"]) {
        if ([[GlobalManager shareGlobalManager].supportCitiesArray containsObject:[receiver objectForKey:@"city"]]) {
            if (!self.isDetail) {
                self.sixView.hidden = NO;
                self.sixViewHeightLayout.constant = 70;
            }
        }else{
            self.sixView.hidden = YES;
            self.sixViewHeightLayout.constant = 0;
        }
    }else{
        self.sixView.hidden = YES;
        self.sixViewHeightLayout.constant = 0;
    }
    [self.tableView reloadData];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark - UITableViewDataSource and UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (self.leasedArr.count > 0 && self.purchasedArr.count > 0) {
        return 2;
    }else{
        return 1;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        if (self.leasedArr.count > 0) {
            return self.leasedArr.count;
        }else{
            return self.purchasedArr.count;
        }
    }else if (section == 1){
        return self.purchasedArr.count;
    }else{
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DHCart_LeasedItemCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHCart_LeasedItemCell forIndexPath:indexPath];
    if (indexPath.section == 0) {
        if (self.leasedArr.count > 0) {
            NSDictionary *dic = [self.leasedArr objectAtIndex:indexPath.row];
            [cell updateCell:dic];
        }else{
            NSDictionary *dic = [self.purchasedArr objectAtIndex:indexPath.row];
            [cell updateCell:dic];
        }
    }else if (indexPath.section == 1){
        NSDictionary *dic = [self.purchasedArr objectAtIndex:indexPath.row];
        [cell updateCell:dic];
    }
    cell.selectedButtonWidthLayout.constant = 15;
    cell.selectedButton.userInteractionEnabled = NO;
    [cell.selectedButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 46)];
    headerView.backgroundColor = [UIColor whiteColor];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, headerView.width - 16, headerView.height)];
    titleLabel.font = [UIFont systemFontOfSize:14];
    titleLabel.textColor = [UIColor blackColor];
    [headerView addSubview:titleLabel];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, headerView.height - 1, headerView.width, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#ECF2F1"];
    [headerView addSubview:lineView];
    
    if (section == 0) {
        titleLabel.text = @"订阅";
        if (self.leasedArr.count == 0) {
            titleLabel.text = @"购买";
        }
    }else if (section == 1){
        titleLabel.text = @"购买";
    }else{
        titleLabel.text = @"洗衣";
    }
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 46;
}

#pragma mark - arr
- (NSArray *)leasedArr{
    if (!_leasedArr) {
        _leasedArr = [NSMutableArray array];
    }
    return _leasedArr;
}

- (NSArray *)purchasedArr{
    if (!_purchasedArr) {
        _purchasedArr = [NSMutableArray array];
    }
    return _purchasedArr;
}

@end
